package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @title: ShoppingCartController
 * @Author 椎名真白
 * @Date: 2022-05-02 9:48
 * @Version 1.0
 */

@RestController
@Slf4j
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * @description: 添加购物车
     * @author: 椎名真白
     * @date: 2022-05-03 18:10
     * @param: shoppingCart
     * @return: com.itheima.reggie.common.R<java.lang.String>
     **/
    @PostMapping("add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());
        queryWrapper.eq(shoppingCart.getDishId()!= null, "dish_id", shoppingCart.getDishId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        if (cart == null) {
            //cart为空 在购物车表中添加一条数据
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }else {
            //cart 不为空 进行跟新操作 将number++
            cart.setNumber(cart.getNumber()+1);
            shoppingCartService.updateById(cart);
        }
        return R.success("添加成功");
    }

    /**
     * @description:  分页查询
     * @author: 椎名真白
     * @date: 2022-05-03 18:11
     * @return: com.itheima.reggie.common.R<java.util.List<com.itheima.reggie.pojo.ShoppingCart>>
     **/
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        //查询条件 当前登录的用户的购物车 获取其id
        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        queryWrapper.orderByDesc("create_time");
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * @description: 清空购物车
     * @author: 椎名真白
     * @date: 2022-05-03 18:13
     * @return: com.itheima.reggie.common.R<java.lang.String>
     **/
    @DeleteMapping("clean")
    public R<String> clean(){
        //根据当前线程的id删除
        //sql delete * from ShoppingCart where user_id=?
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空成功");
    }

    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());
        queryWrapper.eq(shoppingCart.getDishId()!= null, "dish_id", shoppingCart.getDishId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if(cart.getNumber()>1){
        cart.setNumber(cart.getNumber()-1);
        shoppingCartService.updateById(cart);
        }else {
            shoppingCartService.remove(queryWrapper);
        }
        return R.success("减少成功");
    }


}
